﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
namespace UCOLOR.Extend
{
    public  class BMP
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="filePath"></param>
        public static void SavePic(System.IO.Stream stream,string filePath)
        {
            Image img = System.Drawing.Bitmap.FromStream(stream);
            img.Save(filePath);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static Stream ReadFileToStream(string filePath)
        {
            //string ss="";
            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            byte[] b = new byte[fs.Length]; fs.Read(b, 0, b.Length); 
            Stream s = new MemoryStream(b,0,b.Length);
            return s;
        }

        public static void SavePic(Byte[] b,string filePath)
        {
           Stream stream = new MemoryStream(b);
           SavePic(stream,filePath);
        }


        /// <summary> 
        /// 将 Stream 写入文件 
        /// </summary> 
        public static void StreamToFile(Stream stream, string fileName)
        {
            // 把 Stream 转换成 byte[] 
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            // 设置当前流的位置为流的开始 
            stream.Seek(0, SeekOrigin.Begin);
            // 把 byte[] 写入文 件
            FileStream fs = new FileStream(fileName, FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(bytes);
            bw.Close();
            fs.Close();
        }

        /// <summary> 
        /// 将 Stream 转成 byte[] 
        /// </summary> 
        public static byte[] StreamToBytes(Stream stream) 
        { 
            byte[] bytes = new byte[stream.Length]; 
            stream.Read(bytes, 0, bytes.Length); 

            // 设置当前流的位置为流的开始 
            stream.Seek(0, SeekOrigin.Begin); 
            return bytes; 
        } 


    }
}
